﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using Brouteforce.managers;
//using Brouteforce.utilities;
//using Microsoft.Xna.Framework;
//using Brouteforce.gameObjs.gamePiece;

//namespace Brouteforce.util {
//    public static class Tiles {
//        private static int[,] pathLengths;


//        public static bool CanMoveTo(Vector2 from, Vector2 to) {
//            if ((Math.Abs(from.X - to.X) > 1) ||
//                (Math.Abs(from.Y - to.Y) > 1) ||
//                ((Math.Abs(from.X - to.X) == 1) && (Math.Abs(from.Y - to.Y) == 1))||
//                (WorldManager.GetTileAt(to).ContainOtherThan(typeof(Tower)))) {
//                return false;
//            }
//            return true;
//        }

//        public static List<Vector2> FindPath(Vector2 startTile, Vector2 endTile, int steps) {
//            //Abort if start or end tile is null
//            if (startTile.X < 0 || startTile.X > WorldManager.World.GetLength(0)-1 || startTile.Y < 0 || startTile.Y > WorldManager.World.GetLength(1)-1 ||
//                endTile.X < 0 || endTile.X > WorldManager.World.GetLength(0)-1 || endTile.Y < 0 || endTile.Y > WorldManager.World.GetLength(1)-1)
//                return new List<Vector2>();

//            ////Abort if the end tile is non-stoppable
//            //if (!endTile.IsStoppable)
//            //    return new List<Tile>();

//            //Initialize the path length array
//            pathLengths = new int[WorldManager.World.GetLength(0), WorldManager.World.GetLength(1)];
            
//            for (int y = 0; y < pathLengths.GetLength(1); y++)
//                for (int x = 0; x < pathLengths.GetLength(0); x++)
//                    pathLengths[x, y] = int.MaxValue;

//            //Begin at the start tile
//            pathLengths[(int)startTile.X, (int)startTile.Y] = 0;
//            Spread(startTile, steps);

//            //Once done, backtrack from the end tile
//            List<Vector2> result = Trace(endTile);

//            //Only return the path if it contains the start tile
//            if (result.Contains(startTile))
//                return result;
//            return new List<Vector2>();
//        }


//        private static void Spread(Vector2 tile, int steps) {
//            Spread(tile, GetTopSibling(tile), steps);
//            Spread(tile, GetLeftSibling(tile), steps);
//            Spread(tile, GetRightSibling(tile), steps);
//            Spread(tile, GetBottomSibling(tile), steps);
//        }

//        private static void Spread(Vector2 tile, Vector2 target, int steps) {
//            //Abort if any tile is null
//            if (tile == null || target == null || tile.X < 0 || tile.X > WorldManager.World.GetLength(0)-1 || tile.Y < 0 || tile.Y > WorldManager.World.GetLength(1)-1 ||
//                target.X < 0 || target.X > WorldManager.World.GetLength(0)-1 || target.Y < 0 || target.Y > WorldManager.World.GetLength(1)-1)
//                return;

//            //Abort if no movement is allowed
//            if (!CanMoveTo(tile,target))
//                return;

//            if (steps <= 0)
//                return;

//            //Get current path lengths
//            int tileLength = GetPathLength(tile);
//            int targetLength = GetPathLength(target);

//            //Use length if it improves target
//            if (tileLength + 1 < targetLength) {
//                pathLengths[(int)target.X, (int)target.Y] = tileLength + 1;
//                Spread(target, steps-1);
//            }
//        }

//        private static List<Vector2> Trace(Vector2 tile) {
//            //Find the sibling paths
//            int tileLength = GetPathLength(tile);
//            int topLength = GetPathLength(GetTopSibling(tile));
//            int leftLength = GetPathLength(GetLeftSibling(tile));
//            int rightLength = GetPathLength(GetRightSibling(tile));
//            int bottomLength = GetPathLength(GetBottomSibling(tile));

//            //Calculate the lowest path length
//            int lowestLength =
//               Math.Min(tileLength,
//               Math.Min(topLength,
//               Math.Min(leftLength,
//               Math.Min(rightLength, bottomLength))));

//            //Add each possible path
//            List<Vector2> possiblePaths = new List<Vector2>();
//            if (topLength == lowestLength)
//                possiblePaths.Add(GetTopSibling(tile));
//            if (leftLength == lowestLength)
//                possiblePaths.Add(GetLeftSibling(tile));
//            if (rightLength == lowestLength)
//                possiblePaths.Add(GetRightSibling(tile));
//            if (bottomLength == lowestLength)
//                possiblePaths.Add(GetBottomSibling(tile));

//            //Continue through a random possible path
//            List<Vector2> result = new List<Vector2>();
//            if (possiblePaths.Count() > 0)
//                result = Trace(possiblePaths[Randoms.Between(0, possiblePaths.Count()-1)]);

//            //Add the tile itself, then return
//            result.Add(tile);
//            return result;
//        }

//        private static int GetPathLength(Vector2 tile) {
//            if (tile.X < 0 || tile.X > WorldManager.World.GetLength(0)-1 || tile.Y < 0 || tile.Y > WorldManager.World.GetLength(1)-1)
//                return int.MaxValue;
//            return pathLengths[(int)tile.X, (int)tile.Y];
//        }


//        public static Vector2 GetBottomSibling(Vector2 tile) {
//            return new Vector2(tile.X, tile.Y + 1);
//        }

//        public static Vector2 GetRightSibling(Vector2 tile) {
//            return new Vector2(tile.X + 1, tile.Y);
//        }

//        public static Vector2 GetLeftSibling(Vector2 tile) {
//            return new Vector2(tile.X - 1, tile.Y);
//        }

//        public static Vector2 GetTopSibling(Vector2 tile) {
//            return new Vector2(tile.X, tile.Y - 1);
//        }

//        public static Vector2 GetVelocity(Vector2 from, Vector2 to) {
//            if (GetBottomSibling(from) == to) return new Vector2(0,1);
//            if (GetTopSibling(from) == to) return new Vector2(0, -1);
//            if (GetLeftSibling(from) == to) return new Vector2(-1, 0);
//            if (GetRightSibling(from) == to) return new Vector2(1, 0);
//            return Vector2.Zero;
//        }



//    }
//}
